package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.TsCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		//获取视图中的所有数据
		List<BaseCategoryView> categoryViewList = baseCategoryViewMapper.selectList(null);

		List<JSONObject> jsonObjectList=new ArrayList<>(categoryViewList.size());

		Map<Long, List<BaseCategoryView>> map = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()){
			//获取数据
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category1Id = entry.getKey();
			List<BaseCategoryView> category1List = entry.getValue();
			String category1Name = category1List.get(0).getCategory1Name();
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",category1Id);
			category1.put("categoryName",category1Name);
			//获取二级分类
			Map<Long, List<BaseCategoryView>> map1 = category1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
			List<JSONObject> category1ChildList = new ArrayList<>();
			while (iterator1.hasNext()){
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				List<BaseCategoryView> category2List = entry1.getValue();
				JSONObject category2 = new JSONObject();
				category2.put("categoryId",category2Id);
				category2.put("categoryName",category2List.get(0).getCategory2Name());
				List<JSONObject> category2ChildList = category2List.stream().map(baseCategoryView -> {
					//获取三级分类
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				category2.put("categoryChild",category2ChildList);
				category1ChildList.add(category2);
			}
			category1.put("categoryChild",category1ChildList);
			jsonObjectList.add(category1);
		}
		return jsonObjectList;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		List<BaseAttribute> attributeList = baseAttributeMapper.selectBaseAttributeList(category1Id);
		return attributeList;
	}

	@Override
	@TsCache(prefix = "album:category:")
	public BaseCategoryView getCategory(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//初始化返回对象
		JSONObject jSONObject = new JSONObject();
		//获取对象具体信息
		List<BaseCategoryView> categoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>()
				.eq(BaseCategoryView::getCategory1Id, category1Id));
		//封装对象
		Map<Long, List<BaseCategoryView>> map2 = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		List<JSONObject> category1ChildList = new ArrayList<>(map2.size());
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator2 = map2.entrySet().iterator();
		while (iterator2.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> next = iterator2.next();
			Long category2Id = next.getKey();
			List<BaseCategoryView> category2List = next.getValue();
			List<JSONObject> category2ChildList = new ArrayList<>(category2List.size());
			for (BaseCategoryView view : category2List) {
				JSONObject jsonObject3 = new JSONObject();
				jsonObject3.put("categoryId",view.getCategory3Id());
				jsonObject3.put("categoryName",view.getCategory3Name());
				category2ChildList.add(jsonObject3);
			}
			JSONObject jsonObject2 = new JSONObject();
			jsonObject2.put("categoryId",category2Id);
			jsonObject2.put("categoryName",category2List.get(0).getCategory2Name());
			jsonObject2.put("categoryChild",category2ChildList);
			category1ChildList.add(jsonObject2);
		}
		jSONObject.put("categoryId",category1Id);
		jSONObject.put("categoryName",categoryViewList.get(0).getCategory1Name());
		jSONObject.put("categoryChild",category1ChildList);
		return jSONObject;
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		List<BaseCategory2> category2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
				.eq(BaseCategory2::getCategory1Id, category1Id)
				.select(BaseCategory2::getId));
		Assert.notEmpty(category2List,"没有二级分类");
		List<Long> category2IdList = category2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		return baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, category2IdList)
				.eq(BaseCategory3::getIsTop, 1)
				.last("limit 7"));
	}

	@Override
	public List<Long> getCategory1Id() {
		List<BaseCategory1> category1List = baseCategory1Mapper.selectList(null);
		return category1List.stream().map(BaseCategory1::getId).collect(Collectors.toList());
	}
}
